package guowenlong.golf.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.entity.LoginEntity;
import guowenlong.golf.ui.regist.RegistActivity;
import guowenlong.golf.widget.GolfDialog;
import guowenlong.golf.widget.IOSEditText;
import me.wenlong.mixutils.entity.User;
import me.wenlong.mixutils.mvp.BaseActivity;
import me.wenlong.mixutils.util.SpUtil;

/**
 * des   : 描述 author: Administrator email : guowenlong20000@gmail.com time  : 2017年02月09日 下午 4:14.
 */

public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements
    LoginContract.View, View.OnTouchListener {

  @BindView(R.id.tv_phonenumber)
  IOSEditText etPhonenumber;
  @BindView(R.id.tv_password)
  IOSEditText etPassword;
  @BindView(R.id.tv_message)
  TextView tvMessage;
  @BindView(R.id.btn_login)
  Button btnLogin;
  @BindView(R.id.btn_regist)
  Button btnRegist;
  @BindView(R.id.ll_login)
  LinearLayout llLogin;

  @Override
  protected int getContentView(Bundle savedInstanceState) {
    return R.layout.activity_login;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    llLogin.setOnTouchListener(this);
    etPassword.setTextChangedAfterListener(new IOSEditText.TextChangedAfterListener() {
      @Override
      public void onTextChangedAfter(Editable s) {
        tvMessage.setText("");
        if (etPhonenumber.getText().toString().trim().length() == 11
            && etPassword.getText().toString().trim().length() >= 6) {
          btnLogin.setBackgroundResource(R.drawable.bg_green_login);
          btnLogin.setClickable(true);
        } else {
          btnLogin.setBackgroundResource(R.drawable.bg_dark_green_login);
          btnLogin.setClickable(false);
        }
      }
    });
    etPhonenumber.setTextChangedAfterListener(new IOSEditText.TextChangedAfterListener() {
      @Override
      public void onTextChangedAfter(Editable s) {
        tvMessage.setText("");
        if (etPhonenumber.getText().toString().trim().length() == 11
            && etPassword.getText().toString().trim().length() >= 6) {
          btnLogin.setBackgroundResource(R.drawable.bg_green_login);
          btnLogin.setClickable(true);
        } else {
          btnLogin.setBackgroundResource(R.drawable.bg_dark_green_login);
          btnLogin.setClickable(false);
        }
      }
    });
  }

  @Override
  protected void initData() {
    setResult(RESULT_CANCELED);
  }

  @OnClick({R.id.btn_login, R.id.btn_regist})
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.btn_login:
        mPresenter.login(etPhonenumber.getText().toString(), etPassword.getText().toString());
        break;
      case R.id.btn_regist:
        startActivityForResult(new Intent(LoginActivity.this, RegistActivity.class),
            C.ACTIVITY_LOGIN);
        break;
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case C.ACTIVITY_LOGIN:
          finish();
          break;
        default:
          break;
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(etPhonenumber.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(etPassword.getWindowToken(), 0);
    return true;
  }

  @Override
  public void loginSuccess(final LoginEntity loginEntity) {
    final GolfDialog golfDialog = new GolfDialog(LoginActivity.this);
    golfDialog.setMessage("登陆成功");
    golfDialog.show();
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        golfDialog.dismiss();
        User user = new User();
        user.setAuthToken(loginEntity.getToken());
        user.setPhone(etPhonenumber.getText().toString());
        SpUtil.setUser(user);
        finish();
      }
    }, 1200);
  }

  @Override
  public void loginError(LoginEntity loginEntity) {
    tvMessage.setText(loginEntity.getError_message());
    Toast.makeText(this, loginEntity.getError_message(), Toast.LENGTH_SHORT).show();
  }
}